<?php
/**
 * cloudtrip
 * ============================================================================
 * 版权所有 2016-2026 欣才开裆裤开发组
 * 网站地址: http://git.oschina.net/ts128
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 。
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 幸福的路痴
 * Date: 2016-10-20
 */
namespace Admin\Controller;
use Admin\Controller\BaseController;
/**
* 配置项管理
*/
class SysconfigController extends BaseController{
    private $perpage = 10;
    private $M;
    public function _initialize(){
        $this->M = M("sysconfig");
    }
    /*
    *   分页显示
    */
    public function index(){
        $count = $this->M->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        $nowpage = I("p");
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $list = $this->M->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        session_start();
        $id=I("get.");
        $id=$id["id"];
        $_SESSION["pid"]=$id;
        $id=$_SESSION["pid"];
        $M=M("admin_node");
        $node=$M->where("parent_id='$id'")->select();
        $_SESSION["node"]=$node;
        $this->display();
    }
    public function edit(){
        $id = I("id");
        if(!empty($id)){
            $data = $this->M->where("id=".$id)->select();
            $data = $data[0];
            $this->assign("data", $data);
        }
        $this->display();
    }
    /*
    *   添加/修改配置项
    */
    public function doEdit(){
        $data = $this->M->create();
        if(empty($data["id"])){
            $res = $this->M->data($data)->add();
        }
        else{
            $res = $this->M->data($data)->save();
        }
        if($res){
            $this->success("操作成功", U("Sysconfig/index"));
        }
        else{
            $this->error("操作失败", U("Sysconfig/edit"));
        }
    }
    /*
    *   保存所有配置项
    */
    public function doSave(){
        $data = I("post.");
        foreach($data as $k=>$v){
            $this->M->where("varname='$k'")->save(array("value"=>$v));
        }
        $this->success("成功保存", U("Sysconfig/index"));
    }
    /*
    *   删除配置项
    */
    public function del(){
        $id = I("id");
        $res = $this->M->where("id=".$id)->delete();
        if($res){
            $this->success("操作成功", U("Sysconfig/index"));
        }
        else{
            $this->error("操作失败", U("Sysconfig/index"));
        }
    }
}